package com.neiquan.weiguan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.neiquan.weiguan.R;
import com.neiquan.weiguan.activity.ContainerActivity;
import com.neiquan.weiguan.adapter.FragmentAdapter;
import com.neiquan.weiguan.bean.ChannelItem;
import com.neiquan.weiguan.colorful.Colorful;
import com.neiquan.weiguan.fragment.base.AppBaseFragment;
import com.neiquan.weiguan.fragment.view.HomeTabFragmentView;
import com.neiquan.weiguan.presenter.ChannelPresenter;
import com.neiquan.weiguan.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by wyl on 2016/6/2.
 */
public class HomeTabFragment extends AppBaseFragment implements HomeTabFragmentView {


    @InjectView(R.id.tab_hometab_tabs)
    TabLayout mTabs;
    @InjectView(R.id.button_more_columns)
    ImageView mButtonMoreColumns;
    @InjectView(R.id.hometab_viewpager)
    ViewPager mMViewPager;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private ChannelPresenter channelPresenter;
    private FragmentAdapter mAdapetr;
    /**
     * 数据是否发生改变 ture已改变 false未改变默认未改变
     */
    private boolean dataChange = false;
    /**
     * 栏目编辑页返回的position位置页
     */
    private int position;
    private String[] title;

    @Override
    public View initRootView() {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_hometab, null);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void setViews() {

    }

    @Override
    public void initData() {
        channelPresenter = new ChannelPresenter(getActivity(), this);
        channelPresenter.getUserChannel();
        setupColorful();
    }

    @Override
    protected void setupColorful() {
        colorful = new Colorful.Builder(this)
                .backgroundColor(R.id.lin_hometab_title, R.attr.title_bottom_view_bg)
                .backgroundColor(R.id.rel_hometab_menu, R.attr.menu_bg)
                .backgroundColor(R.id.hometab_viewpager, R.attr.root_view_bg)
                .backgroundDrawable(R.id.edit_home_tab_fragment, R.attr.home_search_edit)
                .create();
    }

    /**
     * 初始化Fragment
     */
    private void initFragment(List<ChannelItem> channelItems) {
        fragments.clear();//清空
        mAdapetr = null;
        int count = channelItems.size();
        title = new String[count];
        for (int i = 0; i < count; i++) {
            Bundle data = new Bundle();
            title[i] = channelItems.get(i).getName();
            data.putString("text", channelItems.get(i).getName());
            NewsFragment newfragment = new NewsFragment();
            newfragment.setArguments(data);
            fragments.add(newfragment);
            mTabs.addTab(mTabs.newTab().setText(title[i]));
            mTabs.getTabAt(i).setIcon(R.drawable.ic_launcher);
        }
        mAdapetr = new FragmentAdapter(getChildFragmentManager(), fragments, title);
        mMViewPager.setAdapter(mAdapetr);
        mMViewPager.setOnPageChangeListener(pageListener);
        mTabs.setOnTabSelectedListener(tabListener);
        //将TabLayout和ViewPager关联起来。
        mTabs.setupWithViewPager(mMViewPager);
        //给TabLayout设置适配器
        // mTabs.setTabsFromPagerAdapter(mAdapetr);
    }

    /**
     * tab切换监听方法
     */
    private TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mMViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };
    /**
     * ViewPager切换监听方法
     */
    public ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            mMViewPager.setCurrentItem(position);
        }
    };

    @Override
    public void onResume() {
        super.onResume();
    }

//    @Override
//    protected void onBroadcastListener() {
//        position = mTabs.getSelectedTabPosition();
//        dataChange = true;
//        channelPresenter.getUserChannel();
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ONACTIVITYRESULT_CHANNEL && resultCode == Constant.ONACTIVITYRESULT_CHANNEL) {
            dataChange = data.getBooleanExtra("dataChange", false);
            if (dataChange) {
                channelPresenter.getUserChannel();
                position = 0;
            }
        }
        if (requestCode == Constant.ONACTIVITYRESULT_CHANNEL && resultCode == Constant.ONACTIVITYRESULT_CHANNELSELECT) {
            position = data.getIntExtra("position", 0);
            dataChange = data.getBooleanExtra("dataChange", false);
            if (dataChange) {
                channelPresenter.getUserChannel();
            } else {
                mMViewPager.setCurrentItem(position);
                mTabs.setScrollPosition(position, 0, true);
            }
        }
    }

    @Override
    public void getMenus(List<ChannelItem> channelItems) {
        initFragment(channelItems);
        if (dataChange) {
            mMViewPager.setCurrentItem(position);
            mTabs.setScrollPosition(position, 0, true);
            dataChange = false;
        }
    }

    @OnClick(R.id.button_more_columns)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_more_columns:
                ContainerActivity.startActivityForResurt(getActivity(), EditMenuFragment.class, null, Constant.ONACTIVITYRESULT_CHANNEL);
                break;
        }
    }
}
